﻿using Base.Api.Models;
using Base.Api.Repository;
using Base.Data.Entity;
using Common.AspNetCore;
using Common.Infrastructure;
using Common.RunModels.FCP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Base.Api.Service
{
    public class AuthDataService : CommonService
    {
        private readonly AuthService authService;
        private readonly BaseRepository baseRepository;

        public AuthDataService(IServiceProvider provider, AuthService auth, BaseRepository baseRep) : base(provider)
        {
            this.authService = auth;
            this.baseRepository = baseRep;
        }

        public async Task<AuthRoleAuthModel> GetAuthRoleAuthAsync(ApiParam apiParam)
        {
            var r = await this.baseRepository.CommonRepository.GetEntityAsync<t_role>(apiParam.Keyword);
            AuthRoleAuthModel ret = new AuthRoleAuthModel
            {
                RoleId = r.id,
                RoleCode = r.code,
                RoleName = r.name
            };
            var forms = await this.CallService.PostListAsync<FCPFormAuth>("FCP", "FCP/GetAllAuthFormByPlatform", new AuthFormByPlatformParam() { OrgId = r.org_id, Lang = apiParam.Lang, PlatformType = r.platform_type });
            var roleAuthModel = await this.GetAuthRoleAuthInfoModelByIdAsync(r.id);
            var menus = await this.GetMenuAsync(r.org_id, r.platform_type);
            if (menus != null && menus.Any())
            {
                ret.Menus.AddRange(menus);
            }

            foreach (var f in forms)
            {
                var raf = roleAuthModel.RoleAuthForms.FirstOrDefault(t => t.fcp_form_id == f.FormId);
                AuthRoleAuthFormModel formModel = new AuthRoleAuthFormModel
                {
                    Form = f,
                    FormId = f.FormId,
                    IsAuth = raf != null
                };
                if (raf != null)
                {
                    formModel.AuthBtnIds.AddRange(roleAuthModel.RoleAuthFormBtns.Where(t => t.role_auth_form_id == raf.id).Select(t => t.btn_id).ToList());
                }

                if (raf != null)
                {
                    formModel.AuthFieldIds.AddRange(roleAuthModel.RoleAuthFormFields.Where(t => t.role_auth_form_id == raf.id).Select(t => t.field_id).ToList());
                }

                ret.Forms.Add(formModel);
            }

            return ret;
        }

        public async Task FullUpdateAuthRoleAuthAsync(AuthRoleAuthModel model)
        {
            if (string.IsNullOrEmpty(model.RoleId))
            {
                return;
            }

            var r = await this.baseRepository.CommonRepository.GetEntityAsync<t_role>(model.RoleId);
            if (r == null)
            {
                return;
            }

            ////取数据
            var roleModel = await this.GetRoleAuthInfoModelByIdAsync(r.id);

            ////变量初始化  
            SaveAuthModel saveModel = new SaveAuthModel();

            ////处理 
            foreach (var form in model.Forms)
            {
                if (form.IsAuth)
                {
                    var temp = roleModel.RoleAuthForms.FirstOrDefault(t => t.fcp_form_id == form.FormId);
                    if (temp == null)
                    {
                        temp = new t_role_auth_form();
                        this.SetEntityId(temp, r.id);
                        temp.org_id = r.org_id;
                        temp.fcp_form_id = form.FormId;
                        temp.role_id = r.id;
                        saveModel.AddRoleAuthForms.Add(temp);
                    }

                    var deleteBtnIds = roleModel.RoleAuthFormBtns.Where(t => t.role_auth_form_id == temp.id && !form.AuthBtnIds.Any(t1 => t1 == t.btn_id)).Select(t => t.id).ToList();
                    if (deleteBtnIds.Any())
                    {
                        saveModel.DeleteRoleAuthFormBtnIds.AddRange(deleteBtnIds);
                    }

                    var deleteFieldIds = roleModel.RoleAuthFormFields.Where(t => t.role_auth_form_id == temp.id && !form.AuthFieldIds.Any(t1 => t1 == t.field_id)).Select(t => t.id).ToList();
                    if (deleteFieldIds.Any())
                    {
                        saveModel.DeleteRoleAuthFormFieldIds.AddRange(deleteFieldIds);
                    }

                    form.AuthBtnIds.ForEach(t =>
                    {
                        var btn = roleModel.RoleAuthFormBtns.FirstOrDefault(t1 => t1.role_auth_form_id == temp.id && t1.btn_id == t);
                        if (btn == null)
                        {
                            btn = new t_role_auth_form_btn();
                            this.SetEntityId(btn, temp.id);
                            btn.role_auth_form_id = temp.id;
                            btn.org_id = temp.org_id;
                            btn.btn_id = t;
                            saveModel.AddRoleAuthFormBtns.Add(btn);
                        }
                    });
                    form.AuthFieldIds.ForEach(t =>
                    {
                        var btn = roleModel.RoleAuthFormFields.FirstOrDefault(t1 => t1.role_auth_form_id == temp.id && t1.field_id == t);
                        if (btn == null)
                        {
                            btn = new t_role_auth_form_field();
                            this.SetEntityId(btn, temp.id);
                            btn.role_auth_form_id = temp.id;
                            btn.org_id = temp.org_id;
                            btn.field_id = t;
                            saveModel.AddRoleAuthFormFields.Add(btn);
                        }
                    });
                }
            }

            foreach (var i in roleModel.RoleAuthForms)
            {
                if (!model.Forms.Any(t => t.IsAuth && t.FormId == i.fcp_form_id))
                {
                    saveModel.DeleteRoleAuthFormIds.Add(i.id);
                    saveModel.DeleteRoleAuthFormBtnIds.AddRange(roleModel.RoleAuthFormBtns.Where(t => t.role_auth_form_id == i.id).Select(t => t.id));
                    saveModel.DeleteRoleAuthFormFieldIds.AddRange(roleModel.RoleAuthFormFields.Where(t => t.role_auth_form_id == i.id).Select(t => t.id));
                }
            }

            await this.SaveAuthModelAsync(saveModel);
        }

        public async Task<AuthMemberAuthModel> GetAuthMemberAuthAsync(string memberId)
        {
            var r = await this.baseRepository.CommonRepository.GetEntityAsync<t_member>(memberId);
            if (r == null || string.IsNullOrEmpty(r.id))
            {
                return null;
            }

            var tokenUser = await this.baseRepository.CommonRepository.GetEntityAsync<t_user>(this.UserToken?.UId);

            var ret = new AuthMemberAuthModel
            {
                Name = r.name,
                Code = r.code,
                Id = r.id
            };
            var roles = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role>(null, t => t.org_id == r.org_id);
            if (roles != null)
            {
                ////Admin账号可以分配管理员
                if (tokenUser != null && tokenUser.code.EndsWith("Admin", StringComparison.OrdinalIgnoreCase))
                {
                }
                else if (!r.code.EndsWith("Admin", StringComparison.OrdinalIgnoreCase))
                {
                    roles = roles.Where(t => t.role_type == 0).ToList();
                }

                ret.Roles.AddRange(roles);
            }

            var rolegroups = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role_group>(null, t => t.org_id == r.org_id);
            if (rolegroups != null && rolegroups.Any())
            {
                ret.RoleGroups.AddRange(rolegroups);
            }

            var authRoles = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_member_auth_role>(this.baseRepository.CommonRepository.GetApiParamById(r.id), t => t.member_id == r.id);
            var authRoleGroups = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_member_auth_role_group>(this.baseRepository.CommonRepository.GetApiParamById(r.id), t => t.member_id == r.id);
            if (authRoles.Any())
            {
                ret.AuthRoleIds.AddRange(authRoles.Select(t => t.role_id));
            }

            if (authRoleGroups.Any())
            {
                ret.AuthRoleGroupIds.AddRange(authRoleGroups.Select(t => t.role_group_id));
            }

            return ret;
        }

        public async Task FullUpdateAuthMemberAuthAsync(AuthMemberAuthModel model)
        {
            var r = await this.baseRepository.CommonRepository.GetEntityAsync<t_member>(model.Id);
            if (r == null)
            {
                return;
            }

            var infoModel = await this.GetUpdateRoleAuthInfoModelByIdAsync(r.id);
            SaveAuthRoleModel saveModel = new SaveAuthRoleModel();

            var deleteBtnIds = infoModel.MemberRoles.Where(t => !model.AuthRoleIds.Any(t1 => t1 == t.role_id)).Select(t => t.id).ToList();
            if (deleteBtnIds.Any())
            {
                saveModel.DeleteMemberAuthRoleIds.AddRange(deleteBtnIds);
            }

            var deleteFieldIds = infoModel.MemberRoleGroups.Where(t => !model.AuthRoleGroupIds.Any(t1 => t1 == t.role_group_id)).Select(t => t.id).ToList();
            if (deleteFieldIds.Any())
            {
                saveModel.DeleteMemberAuthRoleGroupIds.AddRange(deleteFieldIds);
            }

            model.AuthRoleIds.ForEach(t =>
            {
                var btn = infoModel.MemberRoles.FirstOrDefault(t1 => t1.role_id == t);
                if (btn == null)
                {
                    btn = new t_member_auth_role();
                    this.SetEntityId(btn, r.id);
                    btn.member_id = r.id;
                    btn.org_id = r.org_id;
                    btn.role_id = t;
                    saveModel.AddMemberAuthRoles.Add(btn);
                }
            });
            model.AuthRoleGroupIds.ForEach(t =>
            {
                var btn = infoModel.MemberRoleGroups.FirstOrDefault(t1 => t1.role_group_id == t);
                if (btn == null)
                {
                    btn = new t_member_auth_role_group();
                    this.SetEntityId(btn, r.id);
                    btn.member_id = r.id;
                    btn.org_id = r.org_id;
                    btn.role_group_id = t;
                    saveModel.AddMemberAuthRoleGroups.Add(btn);
                }
            });

            await this.SaveAuthRoleModelAsync(saveModel);
        }

        public async Task<AuthRoleGroupAuthModel> GetAuthRoleGroupAuthAsync(string roleGroupId)
        {
            var r = await this.baseRepository.CommonRepository.GetEntityAsync<t_role_group>(roleGroupId);
            var ret = new AuthRoleGroupAuthModel
            {
                Code = r.code,
                Name = r.name,
                Id = r.id
            };
            var roles = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role>(null, t => t.org_id == r.org_id);
            if (roles != null)
            {
                ret.Roles.AddRange(roles);
            }

            var authRoles = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role_group_auth_role>(this.baseRepository.CommonRepository.GetApiParamById(r.id), t => t.role_group_id == r.id);
            if (authRoles.Any())
            {
                ret.AuthRoleIds.AddRange(authRoles.Select(t => t.role_id));
            }

            return ret;
        }

        public async Task FullUpdateAuthRoleGroupAuthAsync(AuthRoleGroupAuthModel model)
        {
            var r = await this.baseRepository.CommonRepository.GetEntityAsync<t_role_group>(model.Id);
            if (r == null)
            {
                return;
            }
            ////取数据
            var groupModel = await this.GetUpdateAuthRoleGroupAuthAsync(r.id);

            ////变量初始化
            var saveModel = new SaveAuthRoleGroupModel();

            var deleteBtnIds = groupModel.RoleGroupRoles.Where(t => !model.AuthRoleIds.Any(t1 => t1 == t.role_id)).Select(t => t.id).ToList();
            if (deleteBtnIds.Any())
            {
                saveModel.DeleteMemberAuthRoleIds.AddRange(deleteBtnIds);
            }

            model.AuthRoleIds.ForEach(t =>
            {
                var btn = groupModel.RoleGroupRoles.FirstOrDefault(t1 => t1.role_id == t);
                if (btn == null)
                {
                    btn = new t_role_group_auth_role();
                    this.SetEntityId(btn, r.id);
                    btn.role_group_id = r.id;
                    btn.org_id = r.org_id;
                    btn.role_id = t;
                    saveModel.AddMemberAuthRoles.Add(btn);
                }
            });
            await this.SaveAuthRoleGroupModelAsync(saveModel);
        }

        /// <summary>
        /// 获取用户所属角色与权限
        /// </summary>
        /// <param name="apiParam"></param>
        /// <returns></returns>
        public async Task<MemberAuthMenuModel> GetAuthRoleMenuAsync(ApiParam apiParam)
        {
            var r = await this.baseRepository.CommonRepository.GetEntityAsync<t_member>(apiParam.Keyword);
            if (r == null || string.IsNullOrEmpty(r.id))
            {
                return null;
            }

            var ret = new MemberAuthMenuModel
            {
                Name = r.name,
                Code = r.code,
                Id = r.id
            };

            // 如果前端有传具体的角色，则需要过滤
            List<string> existRoleIds = new List<string>();
            List<string> existGroupIds = new List<string>();
            if (apiParam.Params != null && apiParam.Params.Count > 0)
            {
                foreach (var param in apiParam.Params)
                {
                    if (param.Key == "role" && param.Value != null)
                    {
                        existRoleIds = param.Value.Split(",").ToList();
                    }

                    if (param.Key == "group" && param.Value != null)
                    {
                        existGroupIds = param.Value.Split(",").ToList();
                    }
                }
            }

            List<string> authRoleIds = new List<string>();
            List<string> authRoleGroupIds = new List<string>();
            List<int> platformIds = new List<int>();
            List<t_role> roles = new List<t_role>();
            var authRoles = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_member_auth_role>(this.baseRepository.CommonRepository.GetApiParamById(r.id), t => t.member_id == r.id);
            var authRoleGroups = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_member_auth_role_group>(this.baseRepository.CommonRepository.GetApiParamById(r.id), t => t.member_id == r.id);
            if (authRoles.Any())
            {
                authRoleIds.AddRange(authRoles.Select(t => t.role_id));
                if (existRoleIds.Count == 0)
                {
                    existRoleIds.AddRange(authRoleIds);
                }

                var roleModel = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role>(t => authRoleIds.Contains(t.id));
                ret.AuthRoles.AddRange(roleModel);

                // 过滤角色
                authRoleIds.RemoveAll(t => !existRoleIds.Contains(t));
                platformIds.AddRange(roleModel.Where(t => existRoleIds.Contains(t.id)).Select(t => t.platform_type));
                roles.AddRange(roleModel.Where(t => existRoleIds.Contains(t.id)).ToList());
            }

            if (authRoleGroups.Any())
            {
                authRoleGroupIds.AddRange(authRoleGroups.Select(t => t.role_group_id));
                if (existGroupIds.Count == 0)
                {
                    existGroupIds.AddRange(authRoleGroupIds);
                }

                var groupInfos = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role_group_auth_role>(t => authRoleGroupIds.Contains(t.role_group_id));
                var roleGroupModel = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role_group>(t => authRoleGroupIds.Contains(t.id));
                ret.AuthRoleGroups.AddRange(roleGroupModel);

                // 过滤角色
                authRoleGroupIds.RemoveAll(t => !existGroupIds.Contains(t));
                var groupRoles = groupInfos.Where(t => authRoleGroupIds.Contains(t.role_group_id)).Select(t => t.role_id);
                authRoleIds.AddRange(groupRoles);
                var roleModel = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role>(t => groupRoles.Contains(t.id));
                platformIds.AddRange(roleModel.Select(t => t.platform_type));
                roles.AddRange(roleModel.ToList());
            }

            platformIds = platformIds.Distinct().ToList();
            authRoleIds = authRoleIds.Distinct().ToList();

            List<FCPFormAuth> forms = new List<FCPFormAuth>();

            // 获取所有平台的表单
            foreach (var platformId in platformIds)
            {
                var formInfos = await this.CallService.PostListAsync<FCPFormAuth>("FCP", "FCP/GetAllAuthFormByPlatform", new AuthFormByPlatformParam() { OrgId = r.org_id, Lang = apiParam.Lang, PlatformType = platformId });
                forms.AddRange(formInfos);
            }
            
            var roleAuthModel = await this.GetAuthRoleAuthInfoModelByIdAsync(authRoleIds);
            var menus = await this.GetMenuAsync(r.org_id, platformIds);
            if (menus != null && menus.Any())
            {
                ret.Menus.AddRange(menus);
            }

            foreach (var f in forms)
            {
                var raf = roleAuthModel.RoleAuthForms.FirstOrDefault(t => t.fcp_form_id == f.FormId);
                AuthRoleAuthFormModel formModel = new AuthRoleAuthFormModel
                {
                    Form = f,
                    FormId = f.FormId,
                    IsAuth = raf != null
                };
                if (raf != null)
                {
                    formModel.AuthBtnIds.AddRange(roleAuthModel.RoleAuthFormBtns.Where(t => t.role_auth_form_id == raf.id).Select(t => t.btn_id).ToList());
                }

                if (raf != null)
                {
                    formModel.AuthFieldIds.AddRange(roleAuthModel.RoleAuthFormFields.Where(t => t.role_auth_form_id == raf.id).Select(t => t.field_id).ToList());
                }

                // 如果是菜单所属平台管理员角色，则所属平台的菜单默认要勾选
                AuthMenuModel menu = ret.Menus.Where(t => t.FCPFormId == f.FormId).FirstOrDefault();
                if (menu != null)
                {
                    var roleInfo = roles.Where(t => t.platform_type == menu.PlatformType && t.role_type == 1).FirstOrDefault();
                    if (roleInfo != null)
                    {
                        formModel.IsAuth = true;
                    }
                }

                ret.Forms.Add(formModel);
            }

            return ret;
        }

        public async Task<AuthRoleAuthModel> GetAuthRoleMember(ApiParam apiParam)
        {
            var r = await this.baseRepository.CommonRepository.GetEntityAsync<t_role>(apiParam.Keyword);
            AuthRoleAuthModel ret = new AuthRoleAuthModel
            {
                RoleId = r.id,
                RoleCode = r.code,
                RoleName = r.name
            };
            List<string> memberIds = new List<string>();
            var memberRoles = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_member_auth_role>(t => t.role_id == r.id);
            if (memberRoles != null && memberRoles.Any())
            {
                memberIds.AddRange(memberRoles.Select(t => t.member_id));
                var members = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_member>(t => memberIds.Contains(t.id));
                if (members != null && members.Any())
                {
                    ret.Members.AddRange(members.Select(t => t.name));
                }
            }

            return ret;
        }

        private async Task<UpdateAuthRoleGroupAuthInfoModel> GetUpdateAuthRoleGroupAuthAsync(string roleGroupId)
        {
            UpdateAuthRoleGroupAuthInfoModel ret = new UpdateAuthRoleGroupAuthInfoModel();
            var roleAuthForms = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role_group_auth_role>(this.baseRepository.CommonRepository.GetApiParamById(roleGroupId), t => t.role_group_id == roleGroupId);
            ret.RoleGroupRoles = roleAuthForms;
            return ret;
        }

        private async Task SaveAuthRoleGroupModelAsync(SaveAuthRoleGroupModel model)
        {
            await this.baseRepository.SaveAuthRoleGroupModelAsync(model);
        }

        private async Task SaveAuthRoleModelAsync(SaveAuthRoleModel model)
        {
            await this.baseRepository.SaveAuthRoleModelAsync(model);
        }

        private async Task<UpdateAuthRoleAuthInfoModel> GetUpdateRoleAuthInfoModelByIdAsync(string memberId)
        {
            UpdateAuthRoleAuthInfoModel model = new UpdateAuthRoleAuthInfoModel();
            var roleAuthForms = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_member_auth_role>(this.baseRepository.CommonRepository.GetApiParamById(memberId), t => t.member_id == memberId);
            var roleAuthFormBtns = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_member_auth_role_group>(this.baseRepository.CommonRepository.GetApiParamById(memberId), t => t.member_id == memberId);
            model.MemberRoles = roleAuthForms;
            model.MemberRoleGroups = roleAuthFormBtns;
            return model;
        }

        private async Task<AuthRoleAuthInfoModel> GetAuthRoleAuthInfoModelByIdAsync(string roleId)
        {
            AuthRoleAuthInfoModel ret = new AuthRoleAuthInfoModel();
            var roleAuthForms = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role_auth_form>(this.baseRepository.CommonRepository.GetApiParamById(roleId), t => t.role_id == roleId);
            ret.RoleAuthForms = roleAuthForms;
            if (!roleAuthForms.Any())
            {
                return ret;
            }

            var roleAuthFormIds = roleAuthForms.Select(t => t.id).ToList();
            var roleAuthFormBtns = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role_auth_form_btn>(this.baseRepository.CommonRepository.GetApiParamById(roleId), t => roleAuthFormIds.Contains(t.role_auth_form_id));
            var roleAuthFormFields = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role_auth_form_field>(this.baseRepository.CommonRepository.GetApiParamById(roleId), t => roleAuthFormIds.Contains(t.role_auth_form_id));
            ret.RoleAuthFormBtns = roleAuthFormBtns;
            ret.RoleAuthFormFields = roleAuthFormFields;
            return ret;
        }

        private AuthMenuModel GetLibMenuModel(t_menuBase menu, List<AuthMenuModel> ret)
        {
            AuthMenuModel o = new AuthMenuModel()
            {
                Label = menu.label,
                Id = menu.id,
                ParentId = menu.parent_id,
                FCPFormId = menu.fcp_form_id,
                Num = menu.num,
                PlatformType = menu.platform_type
            };
            ret.Add(o);
            return o;
        }

        private async Task<List<AuthMenuModel>> GetMenuAsync(string orgId, int clientType)
        {
            var orgs = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_menu>(null, t => t.org_id == orgId && t.platform_type == clientType && t.enable_status == 0);
            List<AuthMenuModel> ret = new List<AuthMenuModel>();
            orgs.ForEach(t =>
            {
                var o = GetLibMenuModel(t, ret);
            });
            return ret;
        }

        private async Task<UpdateRoleAuthInfoModel> GetRoleAuthInfoModelByIdAsync(string roleId)
        {
            UpdateRoleAuthInfoModel ret = new UpdateRoleAuthInfoModel();
            var roleAuthForms = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role_auth_form>(this.baseRepository.CommonRepository.GetApiParamById(roleId), t => t.role_id == roleId);
            ret.RoleAuthForms = roleAuthForms;
            if (roleAuthForms.Any())
            {
                var roleAuthFormIds = roleAuthForms.Select(t => t.id).ToList();
                var roleAuthFormBtns = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role_auth_form_btn>(this.baseRepository.CommonRepository.GetApiParamById(roleId), t => roleAuthFormIds.Contains(t.role_auth_form_id));
                var roleAuthFormFields = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role_auth_form_field>(this.baseRepository.CommonRepository.GetApiParamById(roleId), t => roleAuthFormIds.Contains(t.role_auth_form_id));
                ret.RoleAuthFormBtns = roleAuthFormBtns;
                ret.RoleAuthFormFields = roleAuthFormFields;
            }

            var roleAuthInfos = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role_auth_info>(this.baseRepository.CommonRepository.GetApiParamById(roleId), t => t.role_id == roleId);
            ret.RoleAuthInfos = roleAuthInfos;
            return ret;
        }

        private async Task SaveAuthModelAsync(SaveAuthModel model)
        {
            await this.baseRepository.SaveAuthModelAsync(model);
        }

        private async Task<List<AuthMenuModel>> GetMenuAsync(string orgId, List<int> platformTypes)
        {
            var orgs = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_menu>(null, t => t.org_id == orgId && t.enable_status == 0 && platformTypes.Contains(t.platform_type));
            List<AuthMenuModel> ret = new List<AuthMenuModel>();
            orgs.ForEach(t =>
            {
                var o = GetLibMenuModel(t, ret);
            });
            return ret;
        }

        private async Task<AuthRoleAuthInfoModel> GetAuthRoleAuthInfoModelByIdAsync(List<string> roleIds)
        {
            AuthRoleAuthInfoModel ret = new AuthRoleAuthInfoModel();
            var roleAuthForms = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role_auth_form>(t => roleIds.Contains(t.role_id));
            ret.RoleAuthForms = roleAuthForms;
            if (!roleAuthForms.Any())
            {
                return ret;
            }

            var roleAuthFormIds = roleAuthForms.Select(t => t.id).ToList();
            var roleAuthFormBtns = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role_auth_form_btn>(t => roleAuthFormIds.Contains(t.role_auth_form_id));
            var roleAuthFormFields = await this.baseRepository.CommonRepository.GetEntitiesAsync<t_role_auth_form_field>(t => roleAuthFormIds.Contains(t.role_auth_form_id));
            ret.RoleAuthFormBtns = roleAuthFormBtns;
            ret.RoleAuthFormFields = roleAuthFormFields;
            return ret;
        }
    }
}
